package com.yaozheng.vocationaltraining.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * 一项经验头衔
 */
@EViewGroup(R.layout.item_my_experience_title)
public class ItemMyExperienceTitle extends LinearLayout {
    @ViewById
    ImageView itemIntegralDetailsLeftImage;
    @ViewById
    TextView itemIntegralDetailsRightText, itemIntegralDetailsNameText, itemMyExperienceValueTextView, itemMyTitleValueTextView;
    @ViewById
    View itemMyExperienceTitleContentLayout, itemRootView;

    public ItemMyExperienceTitle(Context context) {
        super(context);
    }

    public ItemMyExperienceTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 加载数据
     *
     * @param itemData
     */
    public void loadData(JSONObject itemData, int position, int experience) {

        String fraction = TypeUtils.getJsonString(itemData, "fraction");
        String name = TypeUtils.getJsonString(itemData, "value");
        boolean isSelect = false;
        try {
            if (fraction != null) {
                if (fraction.indexOf("＞") == 0) {
                    int value1 = Integer.parseInt(fraction.substring(1));

                    if (experience > value1) {
                        isSelect = true;
                    }
                } else {
                    String[] strs = fraction.split("-");
                    int value1 = Integer.parseInt(strs[0]);
                    int value2 = Integer.parseInt(strs[1]);
                    if (experience >= value1 && experience <= value2) {
                        isSelect = true;
                    }
                }
            }
        } catch (Exception ex) {
            ErrorUtils.outErrorLog(ex);
            isSelect = false;
        }

        if (isSelect) {

            itemMyExperienceValueTextView.setText("我的经验值：" + experience);
            itemMyTitleValueTextView.setText("我的头衔：" + name);
            itemIntegralDetailsLeftImage.setSelected(true);
            itemMyExperienceTitleContentLayout.setVisibility(View.VISIBLE);
            itemRootView.setBackgroundColor(getResources().getColor(R.color.pass_through_mode_button_bg_color));
        } else {
            itemMyExperienceValueTextView.setText("我");
            itemMyTitleValueTextView.setText("我");
            itemIntegralDetailsLeftImage.setSelected(false);
            itemMyExperienceTitleContentLayout.setVisibility(View.INVISIBLE);
            itemRootView.setBackgroundColor(getResources().getColor(R.color.default_bg_color));
        }
        itemIntegralDetailsNameText.setText(name);
        itemIntegralDetailsRightText.setText(fraction);
    }

}
